using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.Web.Controls
{
    [PersistChildren(true)]
    [ParseChildren(true)]
    public class ConditionalPlaceHolder : PlaceHolder
    {
        public bool Condition
        {
            get { return (bool)(this.ViewState["Condition"] ?? false); }
            set { ViewState["Condition"] = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Obsolete("Use ThenContent property instead.", true)]
        public PlaceHolder IfContent
        {
            get { return this.ThenContent; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PlaceHolder ThenContent { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PlaceHolder ElseContent { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Condition)
            {
                if (this.ThenContent != null)
                    this.ThenContent.RenderControl(writer);
            }
            else
            {
                if (this.ElseContent != null)
                    this.ElseContent.RenderControl(writer);
            }
        }

        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);
         
            if (this.ThenContent != null)
                this.Controls.Add(this.ThenContent);
            if (this.ElseContent != null)
                this.Controls.Add(this.ElseContent);
        }
    }
}